export const globalfun = {
	uploadone: function(count = 1) {
		return new Promise((resolve, reject) => {
			try {
				uni.chooseImage({
					count: count,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						let tempFilePaths = res.tempFilePaths;
						let header = {
							'token': uni.getStorageSync('userToken') || '',
							'content-type': 'application/x-www-form-urlencoded'
						}
						uni.showLoading({
							title: count === 1 ? '上传中...' :
								`上传中... (0/${tempFilePaths.length})`
						});
						const BASEURL = getApp().globalData.BASEURL;
						// 如果是单张图片，保持原有逻辑
						if (count === 1) {
							uni.uploadFile({
								url: BASEURL + '/api/aliyun/oss/upload',
								filePath: tempFilePaths[0],
								name: 'file',
								header: header,
								success: (res1) => {
									let data = JSON.parse(res1.data);
									console.log(data);
									if (res1.statusCode == 200 && data.code ==
										200) {
										let url = data.url;
										uni.hideLoading();
										resolve(url); // 单张返回字符串
									} else {
										uni.hideLoading();
										uni.showToast({
											title: data.msg || '上传失败',
											icon: "none",
											duration: 1500
										});
										reject(new Error(data.msg || '上传失败'));
									}
								},
								fail: (err) => {
									uni.hideLoading();
									reject(err);
								}
							});
						} else {
							// 多张图片上传逻辑
							let uploadPromises = [];
							let successCount = 0;
							let uploadedUrls = [];

							tempFilePaths.forEach((filePath, index) => {
								let uploadPromise = new Promise((uploadResolve,
									uploadReject) => {
									uni.uploadFile({
										url: BASEURL +
											'/api/aliyun/oss/upload',
										filePath: filePath,
										name: 'file',
										header: header,
										success: (res1) => {
											let data = JSON
												.parse(res1
													.data);
											console.log(
												`第${index + 1}张图片上传结果:`,
												data);

											if (res1
												.statusCode ==
												200 && data
												.code == 200) {
												successCount++;
												uploadedUrls
													.push(data
														.url);

												// 更新上传进度
												uni.showLoading({
													title: `上传中... (${successCount}/${tempFilePaths.length})`
												});

												uploadResolve(
													data.url
												);
											} else {
												uploadReject(
													new Error(
														data
														.msg ||
														`第${index + 1}张图片上传失败`
													));
											}
										},
										fail: (err) => {
											uploadReject(
												new Error(
													`第${index + 1}张图片上传失败: ${err.errMsg}`
												));
										}
									});
								});

								uploadPromises.push(uploadPromise);
							});

							// 等待所有图片上传完成
							Promise.all(uploadPromises.map(p => p.catch(e => e)))
								.then(results => {
									uni.hideLoading();

									// 过滤出成功上传的URL
									let successfulUploads = results.filter(result =>
										typeof result === 'string');
									let failedUploads = results.filter(result =>
										result instanceof Error);

									if (failedUploads.length > 0) {
										console.warn('部分图片上传失败:', failedUploads);
										if (successfulUploads.length > 0) {
											resolve(successfulUploads); // 返回成功上传的图片数组
										} else {
											reject(new Error('所有图片上传失败'));
										}
									} else {
										resolve(successfulUploads); // 返回成功上传的图片数组
									}
								})
								.catch(error => {
									uni.hideLoading();
									uni.showToast({
										title: '上传过程发生错误',
										icon: "none",
										duration: 1500
									});
									reject(error);
								});
						}
					},
					fail: (err) => {
						reject(new Error('选择图片失败: ' + err.errMsg));
					}
				});
			} catch (e) {
				reject(e);
			}
		});
	},
	previewImage(list, index) {
		uni.previewImage({
			current: index,
			urls: list,
			longPressActions: {
				itemList: ['发送给朋友', '保存图片', '收藏'],
				success: function(data) {
					console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
				},
				fail: function(err) {
					console.log(err.errMsg);
				}
			}
		});
	},
	pageGoTs: url => {
		uni.switchTab({
			url
		})
	},
	pageTo: url => {
		uni.navigateTo({
			url
		})
	},
	phoneCall: number => {
		uni.makePhoneCall({
			phoneNumber: number, //仅为示例
			success: function() {
				console.log('success');
			}
		});
	},
	opennavigation: data => {
		uni.openLocation({
			name: data.address_info,
			latitude: parseFloat(data.latitude),
			longitude: parseFloat(data.longitude),
			success: function() {
				console.log('success');
			}
		});
	},
	copyFun: data => {
		uni.setClipboardData({
			data, // 要复制的内容
			success: () => { // 复制成功回调
				console.log('11');
				uni.showToast({
					title: '已复制',
					icon: 'success'
				})
			}
		});
	},
}